%% import raw data, maybe create a loop later, to run all the trails.
n = 8; %%number of muscle.
raw_data = EMG;

data = zeros(length(raw_data),n);
time = zeros(length(raw_data),1);

for i = 1:n
    data(:,i) = raw_data(:,i+2);
end

L = length(data);
for i =1:L
    time(i,1) = i/2000;
end
 % signal length/sampling rate.
%% display raw data
figure;
for i = 1:n
    subplot(n,1,i);
    plot(time, data(:,i));
    xlabel('Time(s)');
    ylabel('Voltage(mv)');
    grid
    hold on
end
hold off
%% FFT - to find cutoff frequencies.
fs = 2000; %%sampling rate. frequency resolution = fs/L.
f = fs*(0:L/2)/L;

figure;
for i = 1:n
   p1 = fft(data(:,1));
   p1 = abs(p1/L);
   p1 = p1(1:L/2+1);
   p1(2:end-1) = 2* p1(2:end-1);
   
   subplot(n,1,i)
   plot(f,p1);
   xlabel('frequency');
   ylabel('intensity');
   title('...'); 
end
%% 4th order butterworth filter.
fnyq = fs/2
fcuthigh = 30; %% decided by FFT.
fcutlow = 100;

[b,a] = butter (4,[fcuthigh,fcutlow]/fnyq,'bandpass')
 for i = 1:n
   data(:,i) = filtfilt(b,a,data(:,i));
 end
 %% full wave rectification
 rec_signal = zeros(length(data),n);
 
 for i =1:n
   rec_signal(:,i) = abs(data(:,i));
 end 
 %% plot final data.
 for i = 1:n
     subplot(n,1,i);
     plot(time,rec_signal(:,i));
     xlabel('time(s)');
     ylabel('voltage(ms)');
     grid on
 end
 